package cn.exrick.xboot.core.config.exception

import cn.exrick.xboot.core.common.utils.ResultUtil
import cn.exrick.xboot.core.common.vo.Result
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.web.bind.annotation.ExceptionHandler
import org.springframework.web.bind.annotation.RestControllerAdvice
import javax.validation.ConstraintViolationException

/**
 * @author  fly
 * @date  2021/3/30 14:57
 * @version 1.0
 */
@RestControllerAdvice
open class BaseException {

    companion object{
        val log: Logger = LoggerFactory.getLogger(BaseException::class.java)
    }

    @ExceptionHandler(FlyException::class)
    fun handleException(flyException: FlyException): Result<Any> = ResultUtil.error(flyException.code, flyException.msg)

    @ExceptionHandler(ConstraintViolationException::class)
    fun handleException(exception: ConstraintViolationException): Result<Any> = ResultUtil.error(
        ExceptionEnum.EXCEPTION_ERROR.code,
        exception.constraintViolations.mapNotNull {
            it.invalidValue.toString().plus(":").plus(it.message)
        }.joinToString { "," }
    )

}